<template>
	<scroll-view class="page">
		<view class="backBox" :style="{top:menuTop+'px'}">
			<u-icon name='arrow-left' @click='$utils.back()' color='#fff' size='30rpx' />
		</view>
		<view style="position: relative;">
			<view class="background"></view>
			<view class="background radius"></view>
			<view class="userBox Skeleton" v-if="loading" />
			<view v-else class="userBox">
				<image :src="reward_records.user.avatar" />
			</view>
		</view>
		<view v-if="loading" class="fontBox Skeleton viewWidth" style="height: 30rpx;" />
		<view v-else class="fontBox">
			{{reward_records.user.nick_name}}
		</view>
		<view v-if="loading" class="fontBox blessing_words Skeleton viewWidth" style="height: 30rpx;" />
		<view class="fontBox blessing_words" v-else>
			{{reward_records.blessing_words?reward_records.blessing_words:'恭喜发财，大吉大利'}}
		</view>
		<view v-if="loading" class="fontBox blessing_words Skeleton viewWidth" style="height: 30rpx;" />
		<view v-else class="fontBox red_packet">
			{{moneyFormat(reward_records.redpacket)}}<text style="font-size: 28rpx;">￥</text>
		</view>
		<view v-if="loading" class="ReplyBox Skeleton" />
		<view class="ReplyBox" @click="openModal" v-else>
			<u-icon name="chat" size='40rpx' color='#BBA374' label='回复对方的话' labelColor='#BBA374' labelSize='35rpx'></u-icon>
		</view>
		<view v-if="redPackets.length > 1&&!loading" class="redPacketsBox" @click="jump">
			<image :src="redPackets[1].user.avatar" mode="aspectFill" />
			<view style="flex: 1;margin-right:10rpx ;" class="flex-row">
				<view style=" max-width: 40%;">
					<u-text lines='1' :text='redPackets[1].user.nick_name' color='#fff' size='24rpx' />
				</view>
				<viwe>发出的红包</viwe>
			</view>
			<u-icon name="red-packet-fill" color='#fff' size='50rpx' />
		</view>
		<hongbao v-if="show" :user='redPackets2[0].user' :redPackets='redPackets2' />
		<root-portal>
			<u-modal :show="modalShow" :showCancelButton='true' @close='modalShow=false' @cancel='modalShow=false' @confirm='update'>
				<view style="width: 100%;">
					<view class="popupTitle">
						<u-icon name='close' color='#fff' />
						<view>
							想说的话
						</view>
						<u-icon name='close' @click='modalShow=false' />
					</view>
					<view style="padding: 0 40rpx;">
						<u--form labelWidth='100' labelPosition="top" :model="form" :rules="rules" ref="uForm">
							<u-form-item label="想说的话:" prop="title" borderBottom>
								<u--input v-model="form.title" placeholder="请输入想说的话" />
							</u-form-item>
						</u--form>
					</view>
				</view>
			</u-modal>
		</root-portal>
	</scroll-view>
</template>

<script>
	import hongbao from '@/components/hongbao';
	export default {
		components: {
			hongbao,
		},
		data() {
			return {
				reply_id:null,
				disabled: false,
				modalShow:false,
				form:{
					title:''
				},
				rules: {
					'title': {
						type: 'string',
						required: true,
						message: '请输入想说的话',
						trigger: ['blur', 'change']
					},
				},
				openid: uni.getStorageSync('openid'),
				redPackets2: [],
				show: false,
				loading: true,
				menuTop: 0,
				redPackets: uni.getStorageSync('redPackets'),
				reward_records: {
					sender: {}
				},
				d: false
			}
		},
		onLoad(op) {
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect() //可以获取小程序胶囊的位置。
			this.menuTop = menuButtonInfo.top
			let obj = {
				openid: this.openid,
				blessing_words: this.redPackets[0].blessing_words,
				receiver_id: this.redPackets[0].receiver_id,
				sender_id: this.redPackets[0].user_id,
				redpacket: this.redPackets[0].redpacket,
			}
			this.$api.Run.Red_packet(this.redPackets[0].run_id, this.redPackets[0].user_id, obj).then(res=>{
				this.reply_id=res.data.redpacket.id
				this.loading=false
			})
			this.reward_records = this.redPackets[0]
		},
		methods: {
			openModal(){
				if(this.disabled)return
				this.modalShow=true
			},
			update(){
				if(this.disabled)return
				this.$refs.uForm.validate().then(res => {
					this.disabled = true
					this.$api.Run.Reply(this.reply_id,this.form).then(res => {
						uni.$u.toast('回复成功')
						this.disabled = false
						this.modalShow = false
					}).catch(err => {
						this.disabled = false
						uni.$u.toast(err.data.message)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			moneyFormat(num) {
				if (num?.toString().includes('.')) return num
				return num + '.00'
			},
			jump() {
				if (this.d) return
				this.d = true
				this.redPackets2 = this.redPackets.filter((item, index) => index != 0)
				this.show = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.flex-row {
		display: flex;
		flex-direction: row;
		position: relative;
	}

	.page {
		display: flex;
		height: calc(100vh - constant(safe-area-inset-bottom) + 10rpx);
		height: calc(100vh - env(safe-area-inset-bottom) + 10rpx);
		flex-direction: column;
		box-sizing: border-box;
	}

	.background {
		background-color: rgb(209, 106, 87);
		height: 200rpx;
	}

	.radius {
		height: 100rpx;
		border-radius: 0 0 100% 100%;
	}

	.userBox {
		border-radius: 100%;
		width: 150rpx;
		height: 150rpx;
		overflow: hidden;
		position: absolute;
		bottom: -75rpx;
		margin: auto;
		left: 0;
		right: 0;
		background-color: #fff;

		>image {
			width: 100%;
			height: 100%;
		}
	}

	.backBox {
		position: fixed;
		left: 40rpx;
		z-index: 1111111111;
	}

	.fontBox {
		font-size: 28rpx;
		font-weight: 600;
		text-align: center;
		margin-top: 100rpx;
	}

	.blessing_words {
		margin-top: 20rpx;
		font-weight: 400;
		color: #aaa;
	}

	.red_packet {
		color: #BBA374;
		margin-top: 20rpx;
		font-size: 130rpx;
	}

	.redPacketsBox {
		background-color: rgb(209, 106, 87);
		border-radius: 8rpx;
		padding-right: 20rpx;
		margin: auto;
		bottom: 20vh;
		height: 100rpx;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		position: fixed;
		overflow: hidden;
		color: #fff;
		width: 350rpx;

		image {
			width: 100rpx;
			height: 100rpx;
			margin-right: 10rpx;
		}
	}

	.ReplyBox {
		width: 300rpx;
		    height: 100rpx;
		    border-radius: 8rpx;
		    background-color: rgba(229, 187, 116, 0.2);
		    margin: 20rpx auto;
		    display: flex;
		    align-items: center;
		    box-sizing: border-box;
		    padding: 10rpx;
	}
</style>